package com.sdtele.tta.operation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.JsonRequest;
import com.sdtele.tta.models.JsonResponse;
import com.sdtele.tta.models.User;
import com.sdtele.tta.models.VoteOperation;
import com.sdtele.tta.models.VoteTask;

public class OperationGetMyVoteList extends Operation {
	public JsonResponse handle(JsonRequest jsonRequest) {
		System.out.println("OperationLogin:req=" + jsonRequest);
		JsonResponse jres = new JsonResponse();
		User curUser = (User) request.getSession().getAttribute("user");
		if (curUser == null) {
			jres.setStatus(1);
			jres.setMessage("用户登录失效,请重新登录");
		} else {

			List<VoteTask> voteTask = Dac.getInstance().getVoteOperation(curUser.getId());
			System.out.println(voteTask);

			List<Map<String, Object>> myvoteList = new ArrayList<Map<String, Object>>();
			if (voteTask != null)
				for (VoteTask vote : voteTask) {
					Map<String, Object> voteList = new HashMap<String, Object>();
					voteList.put("vote", vote.ToVoteTaskOut());
					if (Dac.getInstance().getUserofVotetask(vote.getId()).getName() != null) {
						String owner = Dac.getInstance().getUserofVotetask(vote.getId()).getName();
						voteList.put("owner", owner);
					}
					if (Dac.getInstance().getVoteOperation(vote.getId(), curUser.getId()) != null) {
						long completion = Dac.getInstance().getVoteOperation(vote.getId(), curUser.getId())
								.getCompletion();
						voteList.put("completion", completion);
					}

					myvoteList.add(voteList);
				}

			jres.setStatus(0);
			jres.setMessage("我参与的投票列表");
			jres.setEntity(myvoteList);
		}
		return jres;
	}

}